package com.nft.service.school.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nft.service.school.entity.SysUserEntity;
import com.nft.service.school.resp.SysUserResp;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
@DS("master")
public interface SysUserMapper extends BaseMapper<SysUserEntity> {
    @Select("select a.user_id from sys_user a " +
            "left join sys_user_post b on b.user_id=a.user_id " +
            "left join sys_post c on c.post_id=b.post_id " +
            "where c.post_code not in ('xuesheng') " +
            "and a.user_id=#{userId}")
    public SysUserResp getUserStudent(@Param("userId")Long userId);

    @Select("select post_id from sys_post where post_code='xuesheng'")
    public SysUserResp getStudentPostInfo();

    @Select("select role_id from sys_role where role_key='common'")
    public SysUserResp getStudentRoleInfo();

    @Select("select post_id from sys_user_post where user_id=#{userId}")
    public SysUserResp getUserPostInfo(@Param("userId")Long userId);

    @Insert("insert into sys_user_post(user_id,post_id) values(#{userId}, #{postId})")
    int insertUserPost(@Param("userId")Long userId, @Param("postId")Long postId);

    @Insert("insert into sys_user_role(user_id,role_id) values(#{userId}, #{roleId})")
    int insertUserRole(@Param("userId")Long userId, @Param("roleId")Long roleId);

    @Update("update sys_user set minopen_id=#{openId} where phonenumber=#{phonenumber}")
    int updateUserOpenIdByPhone(@Param("phonenumber")String phonenumber, @Param("openId")String openId);

    @Select("select user_id, dept_id from sys_user where dept_id = #{deptId} and del_flag='0' " +
            "and user_id in (select user_id from sys_user_post where post_id in (select post_id from sys_post where post_code=#{postCode}))")
    public List<SysUserResp> selectUserList(@Param("deptId")Long deptId, @Param("postCode")String postCode);
}
